import json
import os
import logging


# 创建日志记录器
def create_logger():
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s | 文件:%(filename)s,函数:%(funcName)s')
    console_handler = logging.StreamHandler()
    console_handler.setFormatter(formatter)
    logger.addHandler(console_handler)
    return logger


# 加载语言数据
def load_language_data():
    file_path = 'language.json'
    if os.path.exists(file_path) and os.path.getsize(file_path) > 0:
        try:
            with open(file_path, 'r', encoding='utf-8') as f:
                language_data = json.load(f)
            return language_data
        except json.JSONDecodeError as e:
            logger.error(f"JSONDecodeError: Could not parse the file. Error: {e}")
        except Exception as e:
            logger.error(f"Unexpected error occurred while loading file: {e}")
    else:
        logger.warning("File does not exist or the file is empty")


# 写入语言数据
def write_language_data(language_data, key, value):
    language_data[key] = value
    try:
        with open('language.json', 'w', encoding='utf-8') as f:
            json.dump(language_data, f, ensure_ascii=False, indent=4)
    except Exception as e:
        logger.error(f"Error occurred while writing to file: {e}")


logger = create_logger()
language_data = load_language_data()